;;
;; C compiler driver
;;

(in-package :oali.proto)


;; c compiler context class
(defclass c-compiler-context ()
  ((parser-context :initform (error 'ctor-error :entity 'parser-context)
		   :initarg :parser-context :reader parser-context)))

(defmethod funcs ((c-compiler c-compiler-context))
  "generic functions accessor"
  (funcs-map (parser-context c-compiler)))



;;
;; functions
;;

(defmethod gen-binary-op ((c-compiler c-compiler-context) op args)
  (let ((result))
    (loop for arg in args
       do (if result (nconc result (list op arg)) (setf result (list arg))))
    result))


;;
;; compiler accessor
;;

(defun c-compile-func (c-compiler func-id)
  (let ((func-def (gethash func-id (funcs c-compiler)))
	all-args
	body)
    (unless func-def (error 'not-found :entity func-id))

    (setf all-args (parse-lambda-list (lambda-list func-def)))
    (setf body (apply (lambda-func func-def) (list c-compiler all-args)))

    (format t "void ~a (/*~a*/) {/*body: ~a*/}~%" func-id all-args body)
    
    func-def))
